package com.hee.erp.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hee.erp.constant.ConstDictType;
import com.hee.erp.modules.entity.*;
import com.hee.erp.modules.mapper.CarMapper;
import com.hee.erp.modules.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hee.erp.vo.CarInsureListProperty;
import com.hee.erp.vo.CarListParamsVo;
import com.hee.erp.vo.FiltersOption;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author weizehong
 * @since 2022-05-02
 */
@Service
@Transactional
public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements ICarService {

    @Autowired
    ICarBrandService carBrandService;

    @Autowired
    ICarModelService carModelService;

    @Autowired
    ISysCompanyService companyService;

    @Autowired
    ISysDictValueService dictValueService;

    @Autowired
    ICarArchivesService carArchivesService;

    @Autowired
    ICarBuyingFilesService carBuyingFilesService;

    @Autowired
    ICarInsureService carInsureService;

    @Autowired
    ICarCheckService carCheckService;

    @Autowired
    ICarInsureWarnRuleService carInsureWarnRuleService;

    @Autowired
    ICarCheckWarnRuleService carCheckWarnRuleService;

    @Override
    public IPage<Car> listByPage(CarListParamsVo params) {

        Integer pageSize = params.getPageSize();
        Integer current = params.getCurrent();
        IPage<Car> page = new Page<>(current, pageSize);

        QueryWrapper queryWrapper = createQueryWrapper(params);

        this.page(page, queryWrapper);
        setForeignKeys(page.getRecords());

        return page;
    }

    @Override
    public Car getDetailById(String id) {
        Car car = this.getById(id);
        setForeignKeys(car);

        //车辆档案
        CarArchives carArchives = carArchivesService.getByCarId(id);
        car.setArchives(carArchives);

        //购车资料
        if(carArchives != null) {
            List<CarBuyingFiles> carBuyingFilesList = carBuyingFilesService.listByCarId(id);
            carArchives.setCarBuyingFilesList(carBuyingFilesList);
        }

        //保险记录
        List<CarInsure> carInsureList = carInsureService.listByCarId(id);
        car.setInsureList(carInsureList);

        //车检记录
        List<CarCheck> checkList = carCheckService.listByCarId(id);
        car.setCheckList(checkList);

        //保险提醒规则
        List<CarInsureWarnRule> insureWarnRuleList = carInsureWarnRuleService.listByCarId(id);
        car.setInsureWarnRuleList(insureWarnRuleList);

        //车检提醒规则
        List<CarCheckWarnRule> checkWarnRuleList = carCheckWarnRuleService.listByCarId(id);
        car.setCheckWarnRuleList(checkWarnRuleList);


        return car;
    }

    @Override
    public List<FiltersOption> getFiltersOptions() {
        return null;
    }

    @Override
    public void saveDetail(Car entity) {
        //保存或修改t_car
       this.saveOrUpdate(entity);
       //保存或修改t_car_archives
        CarArchives archives = entity.getArchives();
        if(archives != null) {
            archives.setCarId(entity.getId());
            carArchivesService.saveOrUpdate(archives);
            //保存t_car_buying_files
            List<CarBuyingFiles> carBuyingFilesList = archives.getCarBuyingFilesList();
            for (CarBuyingFiles carBuyingFiles : carBuyingFilesList) {
                carBuyingFiles.setCarId(entity.getId());
                if (carBuyingFiles.getId().contains("-")) {
                    carBuyingFiles.setId(null);
                }
            }

            carBuyingFilesService.saveOrUpdateBatch(carBuyingFilesList);
        }


        //保存t_car_insure
        List<CarInsure> carInsureList = entity.getInsureList();
        for (CarInsure carInsure : carInsureList) {
            carInsure.setCarId(entity.getId());
        }
        carInsureService.saveOrUpdateBatch(carInsureList);

        //保存t_car_insure_warn_rule
        List<CarInsureWarnRule> insureWarnRuleList = entity.getInsureWarnRuleList();
        for (CarInsureWarnRule carInsureWarnRule : insureWarnRuleList) {
            carInsureWarnRule.setCarId(entity.getId());
        }
        carInsureWarnRuleService.saveOrUpdateBatch(insureWarnRuleList);

        //保存t_car_check
        List<CarCheck> checkList = entity.getCheckList();
        for (CarCheck carCheck : checkList) {
            carCheck.setCarId(entity.getId());
        }
        carCheckService.saveOrUpdateBatch(checkList);

        //保存t_car_check_warn_rule
        List<CarCheckWarnRule> checkWarnRuleList = entity.getCheckWarnRuleList();
        for (CarCheckWarnRule carCheckWarnRule : checkWarnRuleList) {
            carCheckWarnRule.setCarId(entity.getId());
        }
        carCheckWarnRuleService.saveOrUpdateBatch(checkWarnRuleList);

    }

    private void setForeignKeys(List<Car> records) {
        records.forEach(car -> {
           setForeignKeys(car);
        });
    }

    private void setForeignKeys(Car car) {
        //品牌
        if(car.getBrandId() != null) {
            CarBrand brand = carBrandService.getById(car.getBrandId());
            if (brand != null) {
                car.setBrandName(brand.getBrandName());
            }
        }
        //型号
        if(car.getModelId() != null) {
            CarModel model = carModelService.getById(car.getModelId());
            if (model != null) {
                car.setModelName(model.getModelName());
            }
        }
        //车辆类型
        if(car.getCarType() != null) {
            SysDictValue dictCarType = dictValueService.getByTypeAndValue(ConstDictType.CAR_TYPE, car.getCarType());
            if (dictCarType != null) {
                car.setCarTypeName(dictCarType.getDictName());
            }
        }
        //所有人（归属公司）
        if(car.getBelong() != null) {
            SysCompany company = companyService.getById(car.getBelong());
            if (company != null) {
                car.setBelongName(company.getCompanyName());
            }
        }
        //车辆状态
        if(car.getCarStatus() != null) {
            SysDictValue dictCarStatus = dictValueService.getByTypeAndValue(ConstDictType.CAR_STATUS, car.getCarStatus());  //车辆状态
            if (dictCarStatus != null) {
                car.setCarStatusName(dictCarStatus.getDictName());
            }
        }

    }

    private QueryWrapper createQueryWrapper(CarListParamsVo params) {
        QueryWrapper<Car> queryWrapper = new QueryWrapper<>(new Car());
        return queryWrapper;
    }
}
